// pages/login/login.js
var api = require('../../utils/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    second: 60,
    show_get_code: '获取验证码',
    get_code_status: true,
    phoneNum: '',
    code: '',
    region: ['广东省', '广州市', '海珠区'],
    customItem: '全部'
  },
  inputPhoneNum: function (res) {
    var that = this;
    console.log(res.detail.value);
    that.setData({
      phoneNum: res.detail.value
    })
  },
  check: function () {
    var that = this;
    if (this.data.phoneNum.length == 11) {
      var myreg = /^1[3456789]\d{9}$/;
      if (myreg.test(this.data.phoneNum)) {

        var second = that.data.second;
        var timer = setInterval(function () {
          second--;
          that.setData({
            get_code_status: false,
            second: second
          });
          if (second < 1) {
            clearInterval(timer);
            that.setData({
              get_code_status: true,
              second: 60
            });
          }
        }, 1000)
        that.checkUser();
      } else {
        wx.showToast({
          title: '手机号格式有误',
          icon: 'none',
          duration: 1000
        });
        return;
      }
    } else {
      wx.showToast({
        title: '手机号长度有误',
        icon: 'none',
        duration: 1000
      })
      return;
    }
  },
  //看该用户是否注册
  checkUser: function () {
    var that = this;
    wx.request({
      url: api.CheckUser,
      data: {
        mobile: that.data.phoneNum
      },
      method: 'post',
      success: function (res) {
        if (res.data.code == 200) {
          wx.showToast({
            title: '该手机号已经注册',
            icon: 'none',
            duration: 3000
          })
          return;
        } else {
          that.Sendcode();
        }
      },
    })
  },
  //发送验证码
  Sendcode: function (res) {
    var that = this;
    var second = that.data.second;
    wx.request({
      url: api.SendCode,
      data: {
        mobile: that.data.phoneNum
      },
      method: 'post',
      success: function (res) {
        console.log(res);
      },
    })
  },
  pwdOnBlur: function (e) {
    var pwd = e.detail.value;
    if (pwd.length < 6 || pwd.length > 18) {
      wx.showToast({
        title: '密码格式错误',
        duration: 2000,
        icon: 'none'
      });
    }
    this.setData({
      pwd: e.detail.value
    })
  },
  repwdOnBlur: function (e) {
    var pwd2 = e.detail.value;
    if (pwd2 != this.data.pwd) {
      wx.showToast({
        title: '确认密码有误',
        duration: 2000,
        icon: 'none'
      });
      return false;
    }
    this.setData({
      pwd2: pwd2
    })
  },
  //表单提交
  formSubmit: function (e) {
    var that = this;
    var data = e.detail.value;
    console.log(data);

    if (data.mobile != '' && data.code != '' && data.password != '' && data.repassword != '' && data.role != '') {
      if (data.password.length < 6 | data.password.length > 18) {
        wx.showToast({
          title: '密码格式错误',
          duration: 2000,
          icon: 'none'
        });
        return;
      }
      if (data.password != data.repassword) {
        wx.showToast({
          title: '两次密码不一致',
          duration: 2000,
          icon: 'none'
        });
        return;
      }

      wx.request({
        url: api.Reg,
        data: {
          mobile: data.mobile,
          code: data.code,
          password: data.password,
          role: data.role
        },
        method: 'post',
        success: function (res) {
          console.log(res);
          if (res.data.code == 300) {
            wx.showToast({
              title: '验证码不正确',
              duration: 2000,
              icon: 'none'
            });
          } else if (res.data.code == 200) {
            wx.showToast({
              title: '注册成功',
            })
            setTimeout(function () {
              wx.navigateTo({
                url: '/pages/signin/signin',
              })
            }, 2500)
          } else if (res.data.code == 500) {
            wx.showToast({
              title: '请稍后再试',
              duration: 2000,
              icon: 'none'
            });
          }
        },
      })
    } else {
      wx.showToast({
        title: '请填写完整信息',
        duration: 2000,
        icon: 'none'
      });
      return false;
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: 'radio',
      path: 'page/component/pages/radio/radio'
    }
  },
})